
package com.kugou.game.sdk.ui.widget;

import com.kugou.game.sdk.core.FolderManager;
import com.kugou.game.sdk.core.VipManager;
import com.kugou.game.sdk.ui.activity.RechargeActivity;
import com.kugou.game.sdk.ui.activity.TopicActivity;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.AsyncImageLoader;
import com.kugou.game.sdk.utils.AsyncImageLoader.ImageCallback;
import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 描述:标题栏vip弹窗
 *
 * @author wcj
 * @since 2016年1月14日 下午5:29:05
 */
public class VipPopupWindow extends KGPopupWindow {

    private Context mContext;

    public VipPopupWindow(Context context) {
        super(context);
        mContext = context;

        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(Rx.layout.kg_vip_popupwindow, null);
        setContentView(view);

        final ImageView imageView = (ImageView) findViewById(Rx.id.kg_layer_vip_image);
        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String url = VipManager.getInstance().getVipInfo().getVipTopicUrl();
                if (VipManager.getInstance().getVipInfo() != null && !TextUtils.isEmpty(url)) {
                    mContext.startActivity(new Intent(mContext, TopicActivity.class).putExtra(
                            "Url", url));
                    dismiss();
                }

            }
        });

        String iconUrl = VipManager.getInstance().getVipInfo().getVipAdImgUrl();
        if (!TextUtils.isEmpty(iconUrl)) {
            String imgKey = iconUrl;
            String savePath = FolderManager.IMAGE_CACHE_FOLDER
                    + AppUtil.getFileNameFromUrl(iconUrl);
            Bitmap bitmap = new AsyncImageLoader().loadImage(mContext, imgKey, iconUrl, savePath,
                    new ImageCallback() {

                        @Override
                        public void imageLoaded(Bitmap bitmap, String key) {
                            if (bitmap != null) {
                                imageView.setImageBitmap(bitmap);
                            }
                        }
                    });
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
        }

        TextView textView = (TextView) findViewById(Rx.id.kg_vip_tips_tv);
        if (VipManager.getInstance().isVip()) {
            textView.setText("点击进入游币充值页面");
        } else {
            textView.setText("我要成为VIP");
        }

        textView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RechargeActivity.showYoubiRechargePage(mContext, 5);
                dismiss();
            }
        });
    }

    @Override
    protected void populateActionItems(ArrayList<ActionItem> actionItems) {

    }

    @Override
    protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
        setWidgetSpecs(anchorRect.bottom - AppUtil.dip2px(mContext, 0), false);
        setWidgetSpecsX(anchorRect.left - AppUtil.dip2px(mContext, 140));
    }

    @Override
    protected void showArrow() {

    }

    @Override
    protected void prepareAnimationStyle() {

    }

    @Override
    protected void onShow() {

    }

    @Override
    protected void onDismiss() {

    }

    public void updateBtnText() {
        TextView textView = (TextView) findViewById(Rx.id.kg_vip_tips_tv);
        if (VipManager.getInstance().isVip()) {
            textView.setText("点击进入游币充值页面");
        } else {
            textView.setText("我要成为VIP（点击进入游币充值页面）");
        }
    }
}
